<!--  -->
<template>
  <div>
    <el-dialog @close="close" title="编辑" :visible.sync="dialogVisible" :close-on-click-modal="false" width="600px">
      <el-form :model="labelInfo" v-loading="loading" :rules="rules" ref="labelForm" @submit.native.prevent>
        <el-form-item label="旧标签名称" :label-width="formLabelWidth" prop="otitle">
          <el-input size="medium" v-model="labelInfo.otitle" maxlength="64" style="width:100%;" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="新标签名称" :label-width="formLabelWidth" prop="ntitle">
          <el-input size="medium" v-model="labelInfo.ntitle" maxlength="64" style="width:100%;" autocomplete="off"
            placeholder="输入64个字符以内的名称"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="close">取 消</el-button>
        <el-button size="mini" type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    resetData: {
      type: Function
    }
  },
  data () {
    // 自定义校验
    var checkName = (rule, value, callback) => {
      if (value.trim() === '') {
        callback(new Error('标签名称不能都为空格'))
      } else {
        callback()
      }
    }
    return {
      labelInfo: {
        systemId: null,
        otitle: '',
        ntitle: ''
      },
      rules: {
        ntitle: [
          { required: true, message: '请输入标签名称', trigger: 'blur' },
          { min: 1, max: 64, message: '长度在1到64个字符', trigger: 'blur' },
          { validator: checkName, trigger: 'blur' }
        ]
      },
      formLabelWidth: '120px',
      loading: true,
      dialogVisible: false
    }
  },
  methods: {
    close () {
      if (this.$refs['labelForm']) {
        this.$refs['labelForm'].clearValidate()
      }
      this.labelInfo = {}
      this.loading = true
      this.dialogVisible = false
    },
    show (sysId, oldName) {
      this.dialogVisible = true
      this.loading = false
      this.labelInfo.systemId = sysId
      this.labelInfo.otitle = oldName
    },
    save () {
      this.$refs['labelForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          // 请求写入数据库
          this.$post('/system/product/label-title/update', this.labelInfo).then(response => {
            if (response && response.length > 0) {
              this.resetData(response)
            } else {
              console.error(response)
            }
            this.loading = false
            this.dialogVisible = false
          })
        }
      })
    }
  }
}
</script>
